package com.kgc.skconsumer.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.CwOtherIncomeRefund;
import com.kgc.skconsumer.feign.CwOtherIncomeRefundFeign;
import com.kgc.skconsumer.feign.CwWorkFlowFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/CwOtherIncomeRefundController")
public class CwOtherIncomeRefundController {

    private Logger logger = LoggerFactory.getLogger(CwOtherIncomeRefundController.class);

    @Autowired
    private CwWorkFlowFeign cwWorkFlowFeign;

    @Autowired
    private CwOtherIncomeRefundFeign cwOtherIncomeRefundFeign;

    @RequestMapping("/selectCwOtherIncomeRefundByPage")
    public ResponseMsg selectCwOtherIncomeRefundByPage(@RequestBody Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundController selectCwOtherIncomeRefundByPage params {}",map.toString());
        ResponseMsg responseMsg = cwOtherIncomeRefundFeign.selectCwOtherIncomeRefundByPage(map);
        logger.debug("CwOtherIncomeRefundController responseMsg = {}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectCwOtherIncomeRefundByCode")
    public  ResponseMsg selectCwOtherIncomeRefundByCode(@RequestBody Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundController selectCwOtherIncomeRefundByCode params {}",map.toString());
        ResponseMsg responseMsg=cwOtherIncomeRefundFeign.selectCwOtherIncomeRefundByCode(map);
        logger.debug("CwOtherIncomeRefundController  selectCwOtherIncomeRefundByCode  responseMsg = {}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/updateCwOtherIncomeRefund")
    public  ResponseMsg updateCwOtherIncomeRefund(@RequestBody Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundController updateCwOtherIncomeRefund params {}",map.toString());
        ResponseMsg responseMsg=cwOtherIncomeRefundFeign.updateCwOtherIncomeRefund(map);
        logger.debug("CwOtherIncomeRefundController updateCwOtherIncomeRefund responseMsg = {}",responseMsg);
        return responseMsg;
    }


    @RequestMapping("/insertCwOtherIncomeRefund")
    public  ResponseMsg insertCwOtherIncomeRefund(@RequestBody Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundController insertCwOtherIncomeRefund params {}",map.toString());
        ResponseMsg responseMsg = cwOtherIncomeRefundFeign.insertCwOtherIncomeRefund(map);
        ObjectMapper mapper = new ObjectMapper();
        CwOtherIncomeRefund cwOtherIncomeRefund = mapper.convertValue( map.get("cwOtherIncomeRefund"),CwOtherIncomeRefund.class);
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",cwOtherIncomeRefund.getDocCode());//单据编号
        variables.put("type",cwOtherIncomeRefund.getTypeId());//流程类型
        //创建一个流程实例
        cwWorkFlowFeign.startProcess(variables);
        logger.debug("CwOtherIncomeRefundController insertCwOtherIncomeRefund responseMsg = {}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/deleteCwOtherIncomeRefund")
    public  ResponseMsg deleteCwOtherIncomeRefund(@RequestBody Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundController deleteCwOtherIncomeRefund params {}",map.toString());
        ResponseMsg responseMsg = cwOtherIncomeRefundFeign.deleteCwOtherIncomeRefund(map);
        logger.debug("CwOtherIncomeRefundController deleteCwOtherIncomeRefund responseMsg = {}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/checkCwOtherIncomeRefund")
    public  ResponseMsg checkCwOtherIncomeRefund(@RequestBody Map<String, Object> map) {
        logger.info("CwOtherIncomeRefundController checkCwOtherIncomeRefund params {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("docCode"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cwWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        logger.debug("CwOtherIncomeRefundController checkCwOtherIncomeRefund responseMsg = {}",responseMsg);
        return responseMsg;
    }







}
